package com.lanou.dllo.findfood.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.dllo.findfood.entity.MenuResult;
import com.lanou.dllo.findfood.util.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class MenuBodyDataBaseUtil {
    private SQLiteDatabase database;

    public MenuBodyDataBaseUtil(Context context) {
        init(context);
    }

    private void init(Context context) {
//factory 游标, version
        //构建数据库帮助类用于数据库对象的获取
        //参数二表示数据库名
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, Constant.DATABASE_NAME, null, 1);
        database = helper.getWritableDatabase();
    }

    //以键值对的形式保存
    public void savaHomeViewPager(List<MenuResult> dealsList) {
        for (MenuResult homeTopDeals : dealsList) {
            ContentValues values = new ContentValues();
            values.put(Constant.COLUMN_MENU_BODY_IMG, homeTopDeals.getImg());
            values.put(Constant.COLUMN_MENU_BODY_DESCRIPTION, homeTopDeals.getDescription());
            values.put(Constant.COLUMN_MENU_BODY_KEYWORDS, homeTopDeals.getKeywords());
            values.put(Constant.COLUMN_MENU_BODY_NAME, homeTopDeals.getName());
            database.insert(Constant.TABLE_NAME_MENU_BODY, null, values);


        }
    }

    public void deleHomeViewpager() {
        database.delete(Constant.TABLE_NAME_MENU_BODY, null, null);
    }

    public List<MenuResult> queryHomeViewPageData() {

        Cursor cursor = database.query(Constant.TABLE_NAME_MENU_BODY, null,
                null, null, null, null, null);
        if (cursor != null) {
            List<MenuResult> dealses = new ArrayList<MenuResult>();
            while (cursor.moveToNext()) {
                String desption =  cursor.getString(cursor.getColumnIndex(Constant.COLUMN_MENU_BODY_DESCRIPTION));
                String img = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_MENU_BODY_IMG));
                String keywords = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_MENU_BODY_KEYWORDS));
                String name = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_MENU_BODY_NAME));
                MenuResult deals = new MenuResult();
                deals.setImg(img);
                deals.setKeywords(keywords);
                deals.setName(name);
                dealses.add(deals);
            }
            cursor.close();
            return dealses;
        }


        return null;
    }
}
